package cn.ante.system.controller;

import cn.ante.basic.annotation.PreAuthorize;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.PageList;
import cn.ante.system.domain.DictionaryDetail;
import cn.ante.system.query.DictionaryDetailQuery;
import cn.ante.system.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典明细的api",description="数据字典明细相关的crud功能")
@RestController
@RequestMapping("/dictionaryDetail")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @GetMapping
    @ApiOperation(value = "数据字典明细列表")
    public List<DictionaryDetail> loadAll(){
        return dictionaryDetailService.loadAll();
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询数据字典明细")
    @PreAuthorize(name ="id查询数据字典明细",sn="dictionaryDetail:get")
    public DictionaryDetail loadById(@PathVariable("id") Long id){
        return dictionaryDetailService.loadById(id);
    }

    @PostMapping
    @ApiOperation(value = "分页数据字典明细")
    @PreAuthorize(name ="数据字典明细分页展示",sn="dictionaryDetail:list")
    public PageList<DictionaryDetail> findAll(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){

        return dictionaryDetailService.findAll(dictionaryDetailQuery);
    }

    @PutMapping
    @ApiOperation(value = "保存/修改数据字典明细")
    @PreAuthorize(name ="保存/修改数据字典明细",sn="dictionaryDetail:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody DictionaryDetail dictionaryDetail){
        try{
                if (dictionaryDetail.getId() == null){
                    dictionaryDetailService.add(dictionaryDetail);
                }else{
                    dictionaryDetailService.update(dictionaryDetail);
                }
                return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }


    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id删除数据字典明细")
    @PreAuthorize(name ="根据id删除数据字典明细",sn="dictionaryDetail:delete")
    public AjaxResult remove(@PathVariable("id") Long id){
        try{
            dictionaryDetailService.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除数据字典明细")
    @PreAuthorize(name ="批量删除数据字典明细",sn="dictionaryDetail:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try{
            dictionaryDetailService.batchDel(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

}
